package net.sashag.nadlandroid;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class MessagesAdapter extends ArrayAdapter<Message> {

	public MessagesAdapter(Context context, List<Message> messages) {
		super(context, R.layout.message_row, messages);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			convertView = inflater.inflate(R.layout.message_row, null);
		}
		
		Message message = getItem(position);
		final Apartment apartment = message.getApartment();
		
		TextView txtTitle = (TextView) convertView.findViewById(R.id.txtMessageTitle);
		txtTitle.setText(message.getSenderName());
		TextView txtBody = (TextView) convertView.findViewById(R.id.txtMessageBody);
		txtBody.setText(String.format("Sent: %s\nInterested in your apartment on %s.\n\nEmail: %s\nPhone: %s",
				new SimpleDateFormat("d MMM HH:mm").format(message.getSendTime()),
				apartment.getAddress(), message.getSenderEmail(), message.getSenderPhone()));
		
		ImageButton btnViewApartment = (ImageButton) convertView.findViewById(R.id.btnViewApartment);
		btnViewApartment.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new ApartmentDetailsActivity_.IntentBuilder_(getContext()).apartment(apartment).start();
			}
		});
		
		return convertView;
	}
	
}
